<!DOCTYPE html>

<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">

<title>class Legacy::TJPlurker::ThreadService - TJPlurker API Documentation</title>

<link type="text/css" media="screen" href="../../rdoc.css" rel="stylesheet">

<script type="text/javascript">
  var rdoc_rel_prefix = "../../";
</script>

<script type="text/javascript" charset="utf-8" src="../../js/jquery.js"></script>
<script type="text/javascript" charset="utf-8" src="../../js/navigation.js"></script>
<script type="text/javascript" charset="utf-8" src="../../js/search_index.js"></script>
<script type="text/javascript" charset="utf-8" src="../../js/search.js"></script>
<script type="text/javascript" charset="utf-8" src="../../js/searcher.js"></script>
<script type="text/javascript" charset="utf-8" src="../../js/darkfish.js"></script>


<body id="top" class="class">
<nav id="metadata">
  <nav id="home-section" class="section">
  <h3 class="section-header">
    <a href="../../index.html">Home</a>
    <a href="../../table_of_contents.html#classes">Classes</a>
    <a href="../../table_of_contents.html#methods">Methods</a>
  </h3>
</nav>


  <nav id="search-section" class="section project-section" class="initially-hidden">
  <form action="#" method="get" accept-charset="utf-8">
    <h3 class="section-header">
      <input type="text" name="search" placeholder="Search" id="search-field"
             title="Type to search, Up and Down to navigate, Enter to load">
    </h3>
  </form>

  <ul id="search-results" class="initially-hidden"></ul>
</nav>


  <div id="file-metadata">
    <nav id="file-list-section" class="section">
  <h3 class="section-header">Defined In</h3>
  <ul>
    <li>lib/tjplurker/legacy.rb
  </ul>
</nav>

    
  </div>

  <div id="class-metadata">
    
    <nav id="parent-class-section" class="section">
  <h3 class="section-header">Parent</h3>
  
  <p class="link"><a href="Service.html">Legacy::TJPlurker::Service</a>
  
</nav>

    
    <!-- Method Quickref -->
<nav id="method-list-section" class="section">
  <h3 class="section-header">Methods</h3>

  <ul class="link-list">
    
    <li><a href="#method-c-new">::new</a>
    
    <li><a href="#method-i-final">#final</a>
    
    <li><a href="#method-i-serve">#serve</a>
    
  </ul>
</nav>

  </div>

  <div id="project-metadata">
    <nav id="fileindex-section" class="section project-section">
  <h3 class="section-header">Pages</h3>

  <ul>
  
    <li class="file"><a href="../../GETSTART.html">GETSTART</a>
  
    <li class="file"><a href="../../MAKEROBOT.html">MAKEROBOT</a>
  
    <li class="file"><a href="../../README.html">README</a>
  
  </ul>
</nav>

    <nav id="classindex-section" class="section project-section">
  <h3 class="section-header">Class and Module Index</h3>

  <ul class="link-list">
  
    <li><a href="../../Legacy.html">Legacy</a>
  
    <li><a href="../../Legacy/TJPlurker.html">Legacy::TJPlurker</a>
  
    <li><a href="../../Legacy/TJPlurker/NotificationData.html">Legacy::TJPlurker::NotificationData</a>
  
    <li><a href="../../Legacy/TJPlurker/Robot.html">Legacy::TJPlurker::Robot</a>
  
    <li><a href="../../Legacy/TJPlurker/Service.html">Legacy::TJPlurker::Service</a>
  
    <li><a href="../../Legacy/TJPlurker/ThreadService.html">Legacy::TJPlurker::ThreadService</a>
  
    <li><a href="../../TJP.html">TJP</a>
  
    <li><a href="../../TJP/NotificationData.html">TJP::NotificationData</a>
  
    <li><a href="../../TJP/Robot.html">TJP::Robot</a>
  
    <li><a href="../../TJP/Service.html">TJP::Service</a>
  
    <li><a href="../../TJP/TJPlurker.html">TJP::TJPlurker</a>
  
    <li><a href="../../TJP/ThreadService.html">TJP::ThreadService</a>
  
  </ul>
</nav>

  </div>
</nav>

<div id="documentation">
  <h1 class="class">class Legacy::TJPlurker::ThreadService</h1>

  <div id="description" class="description">
    
  </div><!-- description -->

  
  
  
  <section id="5Buntitled-5D" class="documentation-section">
    

    

    

    

    <!-- Methods -->
    
     <section id="public-class-5Buntitled-5D-method-details" class="method-section section">
      <h3 class="section-header">Public Class Methods</h3>

    
      <div id="method-c-new" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">new</span><span
            class="method-args">(name=self.class.to_s, lasting=60, refresh=1, &proc)</span>
          <span class="method-click-advice">click to toggle source</span>
        </div>
        

        <div class="method-description">
          
          <dl class="rdoc-list note-list"><dt><code>lasting</code>
<dd>
<p>How long in second will a thread lives. If one plurk just be responsed,
then it will live another <code>lasting</code> time.</p>
</dd><dt><code>refresh</code>
<dd>
<p>Interval that checks if thread should terminate.</p>
</dd></dl>
          

          
          <div class="method-source-code" id="new-source">
            <pre><span class="ruby-comment"># File lib/tjplurker/legacy.rb, line 443</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">initialize</span> <span class="ruby-identifier">name</span>=<span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">to_s</span>, <span class="ruby-identifier">lasting</span>=<span class="ruby-value">60</span>, <span class="ruby-identifier">refresh</span>=<span class="ruby-value">1</span>, &amp;<span class="ruby-identifier">proc</span>
    <span class="ruby-keyword">super</span> <span class="ruby-identifier">name</span>
    <span class="ruby-ivar">@lasting</span> = <span class="ruby-identifier">lasting</span>
    <span class="ruby-ivar">@refresh</span> = <span class="ruby-identifier">refresh</span>
    <span class="ruby-ivar">@plurk_id_hash</span> = <span class="ruby-constant">Hash</span>.<span class="ruby-identifier">new</span>
    <span class="ruby-ivar">@mutex</span> = <span class="ruby-constant">Mutex</span>.<span class="ruby-identifier">new</span>
<span class="ruby-keyword">end</span></pre>
          </div><!-- new-source -->
          
        </div>

        

        
      </div><!-- new-method -->

    
    </section><!-- public-class-method-details -->
  
     <section id="public-instance-5Buntitled-5D-method-details" class="method-section section">
      <h3 class="section-header">Public Instance Methods</h3>

    
      <div id="method-i-final" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">final</span><span
            class="method-args">(tjplurker, data)</span>
          <span class="method-click-advice">click to toggle source</span>
        </div>
        

        <div class="method-description">
          
          <p>Invoke after thread termination. You can use this to tell users this
service just terminate.</p>
          

          
          <div class="method-source-code" id="final-source">
            <pre><span class="ruby-comment"># File lib/tjplurker/legacy.rb, line 453</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">final</span> <span class="ruby-identifier">tjplurker</span>, <span class="ruby-identifier">data</span>

<span class="ruby-keyword">end</span></pre>
          </div><!-- final-source -->
          
        </div>

        

        
      </div><!-- final-method -->

    
      <div id="method-i-serve" class="method-detail ">
        
        <div class="method-heading">
          <span class="method-name">serve</span><span
            class="method-args">(tjplurker, data)</span>
          <span class="method-click-advice">click to toggle source</span>
        </div>
        

        <div class="method-description">
          
          
          

          
          <div class="method-source-code" id="serve-source">
            <pre><span class="ruby-comment"># File lib/tjplurker/legacy.rb, line 457</span>
<span class="ruby-keyword">def</span> <span class="ruby-identifier">serve</span> <span class="ruby-identifier">tjplurker</span>, <span class="ruby-identifier">data</span>
    <span class="ruby-keyword">if</span> <span class="ruby-identifier">data</span>.<span class="ruby-identifier">type</span><span class="ruby-operator">==</span><span class="ruby-string">'new_plurk'</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">decide</span>(<span class="ruby-identifier">tjplurker</span>, <span class="ruby-identifier">data</span>)
        <span class="ruby-ivar">@plurk_id_hash</span>[<span class="ruby-identifier">data</span>.<span class="ruby-identifier">plurk_id</span>] = <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">new</span>(<span class="ruby-identifier">data</span>.<span class="ruby-identifier">plurk_id</span>){<span class="ruby-operator">|</span><span class="ruby-identifier">plurk_id</span><span class="ruby-operator">|</span>
            <span class="ruby-identifier">$tjpLog</span>.<span class="ruby-identifier">debug</span>(<span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>){<span class="ruby-node">&quot;Create thread in plurk ##{plurk_id}&quot;</span>}
            <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">current</span>[<span class="ruby-value">:last_time</span>] = <span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span>.<span class="ruby-identifier">to_i</span>
            <span class="ruby-identifier">loop</span>{
                <span class="ruby-identifier">sleep</span>(<span class="ruby-ivar">@refresh</span>)
                <span class="ruby-identifier">$tjpLog</span>.<span class="ruby-identifier">debug</span>(<span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>){<span class="ruby-node">&quot;Plurk ##{plurk_id} remain #{Thread.current[:last_time]+@lasting-Time.now.to_i} seconds.&quot;</span>}
                <span class="ruby-ivar">@mutex</span>.<span class="ruby-identifier">lock</span>
                <span class="ruby-keyword">if</span> <span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span>.<span class="ruby-identifier">to_i</span> <span class="ruby-operator">-</span> <span class="ruby-constant">Thread</span>.<span class="ruby-identifier">current</span>[<span class="ruby-value">:last_time</span>] <span class="ruby-operator">&gt;</span> <span class="ruby-ivar">@lasting</span>
                    <span class="ruby-ivar">@mutex</span>.<span class="ruby-identifier">unlock</span>
                    <span class="ruby-keyword">break</span>
                <span class="ruby-keyword">end</span>
                <span class="ruby-ivar">@mutex</span>.<span class="ruby-identifier">unlock</span>
            }
            <span class="ruby-identifier">$tjpLog</span>.<span class="ruby-identifier">debug</span>(<span class="ruby-keyword">self</span>.<span class="ruby-identifier">class</span>){<span class="ruby-node">&quot;Terminate thread in plurk ##{plurk_id}&quot;</span>}
            <span class="ruby-ivar">@mutex</span>.<span class="ruby-identifier">lock</span>
            <span class="ruby-ivar">@plurk_id_hash</span>.<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">plurk_id</span>)
            <span class="ruby-ivar">@mutex</span>.<span class="ruby-identifier">unlock</span>
            <span class="ruby-identifier">final</span>(<span class="ruby-identifier">tjplurker</span>, <span class="ruby-identifier">data</span>)
        }
    <span class="ruby-keyword">else</span>
        <span class="ruby-ivar">@mutex</span>.<span class="ruby-identifier">lock</span>
        <span class="ruby-keyword">if</span> <span class="ruby-identifier">data</span>.<span class="ruby-identifier">type</span><span class="ruby-operator">==</span><span class="ruby-string">'new_response'</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-ivar">@plurk_id_hash</span>.<span class="ruby-identifier">has_key?</span>(<span class="ruby-identifier">data</span>.<span class="ruby-identifier">plurk_id</span>)
            <span class="ruby-ivar">@plurk_id_hash</span>[<span class="ruby-identifier">data</span>.<span class="ruby-identifier">plurk_id</span>][<span class="ruby-value">:last_time</span>] = <span class="ruby-constant">Time</span>.<span class="ruby-identifier">now</span>.<span class="ruby-identifier">to_i</span>
            <span class="ruby-identifier">action</span>(<span class="ruby-identifier">tjplurker</span>, <span class="ruby-identifier">data</span>)
        <span class="ruby-keyword">end</span>
        <span class="ruby-ivar">@mutex</span>.<span class="ruby-identifier">unlock</span>
    <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
          </div><!-- serve-source -->
          
        </div>

        

        
      </div><!-- serve-method -->

    
    </section><!-- public-instance-method-details -->
  
  </section><!-- 5Buntitled-5D -->

</div><!-- documentation -->


<footer id="validator-badges">
  <p><a href="http://validator.w3.org/check/referer">[Validate]</a>
  <p>Generated by <a href="https://github.com/rdoc/rdoc">RDoc</a> 3.12.
  <p>Generated with the <a href="http://deveiate.org/projects/Darkfish-Rdoc/">Darkfish Rdoc Generator</a> 3.
</footer>

